package com.tyk.bookstore.back.platform.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.platform.model.query.AreaAddQuery;
import com.tyk.bookstore.back.platform.model.query.AreaListQuery;
import com.tyk.bookstore.back.platform.model.query.AreaPageQuery;
import com.tyk.bookstore.back.platform.model.query.AreaUpdateQuery;
import com.tyk.bookstore.back.platform.model.vo.AreaListVo;
import com.tyk.bookstore.back.platform.model.vo.AreaPageVo;
import com.tyk.bookstore.back.platform.model.vo.AreaSimVo;
import com.tyk.bookstore.back.platform.model.vo.AreaVo;
import com.tyk.bookstore.back.platform.service.AreaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author tyk
 * @description 行政区域
 */
@RequestMapping("api/platform/area")
@Tag(name = "平台管理/行政区域管理", description = "行政区域管理")
@RequiredArgsConstructor
@RestController
public class AreaController {

    private final AreaService areaService;

    @Operation(summary = "查询行政划分树", description = "查询行政划分树")
    @SaCheckPermission("platform:area:list:nest")
    @GetMapping({"list/nest"})
    public R<List<AreaSimVo>> getAreaTree() {
        return R.ok(areaService.getAreaSimTree());
    }


    @Operation(summary = "查询行政划分列表", description = "查询行政划分列表")
    @SaCheckPermission("platform:area:list")
    @GetMapping({"list"})
    public R<List<AreaListVo>> getAreaList(
            @Schema(description = "行政区域列表查询参数")
            @ParameterObject
            @Validated AreaListQuery query
    ) {
        return R.ok(areaService.getAreaList(query));
    }

    @Operation(summary = "查询行政区域详情", description = "查询行政区域详情")
    @SaCheckPermission("platform:area:get")
    @GetMapping("{id}")
    public R<AreaVo> getById(
            @Schema(description = "行政区域ID")
            @NotNull @Min(value = 1)
            @PathVariable Long id
    ) {
        return R.ok(areaService.getById(id));
    }

    @Operation(summary = "分页查询行政区域", description = "分页查询行政区域")
    @SaCheckPermission("platform:area:page")
    @GetMapping("page")
    public R<AreaPageVo> getPage(
            @Schema(description = "行政区域分页查询参数")
            @ParameterObject
            @Validated AreaPageQuery query
    ) {
        return R.ok(areaService.getPage(query));
    }

    @Operation(summary = "添加行政区域", description = "添加行政区域")
    @SaCheckPermission("platform:area:add")
    @PostMapping
    public R<Object> add(
            @Validated @RequestBody AreaAddQuery query
    ) {
        areaService.add(query);
        return R.ok();
    }

    @Operation(summary = "更新行政区域", description = "更新行政区域")
    @SaCheckPermission("platform:area:update")
    @PutMapping
    public R<Object> updateById(
            @Validated @RequestBody AreaUpdateQuery query
    ) {
        areaService.updateById(query);
        return R.ok();
    }

    @Operation(summary = "删除行政区域", description = "删除行政区域")
    @SaCheckPermission("platform:area:delete")
    @DeleteMapping("{code}")
    public R<Object> deleteById(
            @Schema(description = "行政区域Code")
            @Min(value = 1) @NotNull
            @PathVariable Long code
    ) {
        areaService.deleteById(code);
        return R.ok();
    }

    @Operation(summary = "批量删除行政区域", description = "批量删除行政区域")
    @SaCheckPermission("platform:area:delete")
    @DeleteMapping
    public R<Object> deleteBatchByIdSet(
            @Schema(description = "行政区域ID列表")
            @NotEmpty @NotNull Set<Long> idList
    ) {
        areaService.deleteBatchByIdSet(idList);
        return R.ok();
    }
}

